<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Polymorphic casts</title>
<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="../conversion.html" title="Chapter 8. The Conversion Library 1.7">
<link rel="prev" href="../conversion.html" title="Chapter 8. The Conversion Library 1.7">
<link rel="next" href="synopsis.html" title="Synopsis">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
<td align="center"><a href="../../../index.html">Home</a></td>
<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../conversion.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../conversion.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="synopsis.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="conversion.polymorphic_casts"></a><a class="link" href="polymorphic_casts.html" title="Polymorphic casts">Polymorphic casts</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="polymorphic_casts.html#conversion.polymorphic_casts.polymorphic_downcast">polymorphic_downcast</a></span></dt>
<dt><span class="section"><a href="polymorphic_casts.html#conversion.polymorphic_casts.polymorphic_cast">polymorphic_cast</a></span></dt>
<dt><span class="section"><a href="polymorphic_casts.html#conversion.polymorphic_casts.polymorphic_pointer_cast">polymorphic_pointer_cast</a></span></dt>
</dl></div>
<p>
      Pointers to polymorphic objects (objects of classes which define at least one
      virtual function) are sometimes downcast or crosscast. Downcasting means casting
      from a base class to a derived class. Crosscasting means casting across an
      inheritance hierarchy diagram, such as from one base to the other in a <code class="literal">Y</code>
      diagram hierarchy.
    </p>
<p>
      Such casts can be done with old-style casts, but this approach is never to
      be recommended. Old-style casts are sorely lacking in type safety, suffer poor
      readability, and are difficult to locate with search tools.
    </p>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="conversion.polymorphic_casts.polymorphic_downcast"></a><a name="polymorphic_downcast"></a><a class="link" href="polymorphic_casts.html#conversion.polymorphic_casts.polymorphic_downcast" title="polymorphic_downcast">polymorphic_downcast</a>
</h3></div></div></div>
<p>
        The C++ built-in <code class="computeroutput"><span class="keyword">static_cast</span></code>
        can be used for efficiently downcasting pointers to polymorphic objects,
        but provides no error detection for the case where the pointer being cast
        actually points to the wrong derived class. The <code class="computeroutput"><span class="identifier">polymorphic_downcast</span></code>
        template retains the efficiency of <code class="computeroutput"><span class="keyword">static_cast</span></code>
        for non-debug compilations, but for debug compilations adds safety via an
        <code class="computeroutput"><span class="identifier">assert</span><span class="special">()</span></code>
        that a <code class="computeroutput"><span class="keyword">dynamic_cast</span></code> succeeds.
      </p>
<p>
        A <code class="computeroutput"><span class="identifier">polymorphic_downcast</span></code> should
        be used for downcasts that you are certain should succeed. Error checking
        is only performed in translation units where <code class="computeroutput"><span class="identifier">NDEBUG</span></code>
        is not defined, via
      </p>
<pre class="programlisting"><span class="identifier">assert</span><span class="special">(</span> <span class="keyword">dynamic_cast</span><span class="special">&lt;</span><span class="identifier">Derived</span><span class="special">&gt;(</span><span class="identifier">x</span><span class="special">)</span> <span class="special">==</span> <span class="identifier">x</span> <span class="special">)</span>
</pre>
<p>
        where <code class="computeroutput"><span class="identifier">x</span></code> is the source pointer.
        This approach ensures that not only is a non-zero pointer returned, but also
        that it is correct in the presence of multiple inheritance. Attempts to crosscast
        using <code class="computeroutput"><span class="identifier">polymorphic_downcast</span></code>
        will fail to compile.
      </p>
<div class="warning"><table border="0" summary="Warning">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Warning]" src="../../../doc/src/images/warning.png"></td>
<th align="left">Warning</th>
</tr>
<tr><td align="left" valign="top"><p>
          Because <code class="computeroutput"><span class="identifier">polymorphic_downcast</span></code>
          uses <code class="computeroutput"><span class="identifier">assert</span><span class="special">()</span></code>,
          it violates the One Definition Rule (ODR) if <code class="computeroutput"><span class="identifier">NDEBUG</span></code>
          is inconsistently defined across translation units. See ISO Std 3.2
        </p></td></tr>
</table></div>
<h5>
<a name="conversion.polymorphic_casts.polymorphic_downcast.h0"></a>
        <span class="phrase"><a name="conversion.polymorphic_casts.polymorphic_downcast.example_"></a></span><a class="link" href="polymorphic_casts.html#conversion.polymorphic_casts.polymorphic_downcast.example_">Example:</a>
      </h5>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">polymorphic_cast</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="special">...</span>
<span class="keyword">class</span> <span class="identifier">Fruit</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">Fruit</span><span class="special">(){};</span> <span class="special">...</span> <span class="special">};</span>
<span class="keyword">class</span> <span class="identifier">Banana</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">Fruit</span> <span class="special">{</span> <span class="special">...</span> <span class="special">};</span>
<span class="special">...</span>
<span class="keyword">void</span> <span class="identifier">f</span><span class="special">(</span> <span class="identifier">Fruit</span> <span class="special">*</span> <span class="identifier">fruit</span> <span class="special">)</span> <span class="special">{</span>
  <span class="comment">// ... logic which leads us to believe it is a Banana</span>
  <span class="identifier">Banana</span> <span class="special">*</span> <span class="identifier">banana</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">polymorphic_downcast</span><span class="special">&lt;</span><span class="identifier">Banana</span><span class="special">*&gt;(</span><span class="identifier">fruit</span><span class="special">);</span>
  <span class="special">...</span>
<span class="special">}</span>
</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="conversion.polymorphic_casts.polymorphic_cast"></a><a name="polymorphic_cast"></a><a class="link" href="polymorphic_casts.html#conversion.polymorphic_casts.polymorphic_cast" title="polymorphic_cast">polymorphic_cast</a>
</h3></div></div></div>
<p>
        The C++ built-in <code class="computeroutput"><span class="keyword">dynamic_cast</span></code>
        can be used for downcasts and crosscasts of pointers to polymorphic objects,
        but error notification in the form of a returned value of 0 is inconvenient
        to test, or worse yet, easy to forget to test. The throwing form of <code class="computeroutput"><span class="keyword">dynamic_cast</span></code>, which works on references, can
        be used on pointers through the ugly expression <code class="computeroutput"><span class="special">&amp;</span><span class="keyword">dynamic_cast</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&amp;&gt;(*</span><span class="identifier">p</span><span class="special">)</span></code>, which
        causes undefined behavior if <code class="computeroutput"><span class="identifier">p</span></code>
        is <code class="computeroutput"><span class="number">0</span></code>. The <code class="computeroutput"><span class="identifier">polymorphic_cast</span></code>
        template performs a <code class="computeroutput"><span class="keyword">dynamic_cast</span></code>
        on a pointer, and throws an exception if the <code class="computeroutput"><span class="keyword">dynamic_cast</span></code>
        returns 0.
      </p>
<p>
        For crosscasts, or when the success of a cast can only be known at runtime,
        or when efficiency is not important, <code class="computeroutput"><span class="identifier">polymorphic_cast</span></code>
        is preferred.
      </p>
<p>
        The C++ built-in <code class="computeroutput"><span class="keyword">dynamic_cast</span></code>
        must be used to cast references rather than pointers. It is also the only
        cast that can be used to check whether a given interface is supported; in
        that case a return of 0 isn't an error condition.
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="conversion.polymorphic_casts.polymorphic_pointer_cast"></a><a name="polymorphic_pointer_cast"></a><a class="link" href="polymorphic_casts.html#conversion.polymorphic_casts.polymorphic_pointer_cast" title="polymorphic_pointer_cast">polymorphic_pointer_cast</a>
</h3></div></div></div>
<p>
        While <code class="computeroutput"><span class="identifier">polymorphic_downcast</span></code>
        and <code class="computeroutput"><span class="identifier">polymorphic_cast</span></code> work
        with built-in pointer types only, <code class="computeroutput"><span class="identifier">polymorphic_pointer_downcast</span></code>
        and <code class="computeroutput"><span class="identifier">polymorphic_pointer_cast</span></code>
        are more generic versions with support for any pointer type for which the
        following expressions would be valid:
      </p>
<p>
        For <code class="computeroutput"><span class="identifier">polymorphic_pointer_downcast</span></code>:
      </p>
<pre class="programlisting"><span class="identifier">static_pointer_cast</span><span class="special">&lt;</span><span class="identifier">Derived</span><span class="special">&gt;(</span><span class="identifier">p</span><span class="special">);</span>
<span class="identifier">dynamic_pointer_cast</span><span class="special">&lt;</span><span class="identifier">Derived</span><span class="special">&gt;(</span><span class="identifier">p</span><span class="special">);</span>
</pre>
<p>
        For <code class="computeroutput"><span class="identifier">polymorphic_pointer_cast</span></code>:
      </p>
<pre class="programlisting"><span class="identifier">dynamic_pointer_cast</span><span class="special">&lt;</span><span class="identifier">Derived</span><span class="special">&gt;(</span><span class="identifier">p</span><span class="special">);</span>
<span class="special">!</span><span class="identifier">p</span><span class="special">;</span> <span class="comment">// conversion to bool with negation</span>
</pre>
<p>
        This includes C++ built-in pointers, <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">shared_ptr</span></code>,
        <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">shared_ptr</span></code>, <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">intrusive_ptr</span></code>,
        etc.
      </p>
<h5>
<a name="conversion.polymorphic_casts.polymorphic_pointer_cast.h0"></a>
        <span class="phrase"><a name="conversion.polymorphic_casts.polymorphic_pointer_cast.example_"></a></span><a class="link" href="polymorphic_casts.html#conversion.polymorphic_casts.polymorphic_pointer_cast.example_">Example:</a>
      </h5>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">polymorphic_pointer_cast</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">class</span> <span class="identifier">Fruit</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">Fruit</span><span class="special">(){}</span> <span class="special">};</span>
<span class="keyword">class</span> <span class="identifier">Banana</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">Fruit</span> <span class="special">{};</span>

<span class="comment">// Use one of these:</span>
<span class="keyword">using</span> <span class="identifier">FruitPtr</span> <span class="special">=</span> <span class="identifier">Fruit</span><span class="special">*;</span>
<span class="keyword">using</span> <span class="identifier">FruitPtr</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">shared_ptr</span><span class="special">&lt;</span><span class="identifier">Fruit</span><span class="special">&gt;;</span>
<span class="keyword">using</span> <span class="identifier">FruitPtr</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">shared_ptr</span><span class="special">&lt;</span><span class="identifier">Fruit</span><span class="special">&gt;;</span>
<span class="keyword">using</span> <span class="identifier">FruitPtr</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">intrusive_ptr</span><span class="special">&lt;</span><span class="identifier">Fruit</span><span class="special">&gt;;</span>

<span class="keyword">void</span> <span class="identifier">f</span><span class="special">(</span><span class="identifier">FruitPtr</span> <span class="identifier">fruit</span><span class="special">)</span> <span class="special">{</span>
  <span class="comment">// ... logic which leads us to believe it is a banana</span>
  <span class="keyword">auto</span> <span class="identifier">banana</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">polymorphic_pointer_downcast</span><span class="special">&lt;</span><span class="identifier">Banana</span><span class="special">&gt;(</span><span class="identifier">fruit</span><span class="special">);</span>
  <span class="special">...</span>
<span class="special">}</span>
</pre>
</div>
</div>
<div class="copyright-footer">Copyright © 2001 Beman Dawes<br>Copyright © 2014-2025 Antony Polukhin<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../conversion.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../conversion.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="synopsis.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
